﻿using Common.Utility;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore;
using SkiaSharp;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using Microsoft.EntityFrameworkCore;

namespace AWSV2.ViewModels
{
    public class PzReportViewModel : Screen
    {
        private IWindowManager windowManager;

        public static readonly SolidColorBrush _titleBrush
            = Application.Current.FindResource("MaterialDesignBody") as SolidColorBrush;


        private SolidColorPaint _legendTextPaint;
        public SolidColorPaint LegendTextPaint
        {
            get { return _legendTextPaint; }
            set { SetAndNotify(ref _legendTextPaint, value); }
        }

        private ISeries[] _seriesCollection;
        public ISeries[] SeriesCollection { get => _seriesCollection; set => SetAndNotify(ref _seriesCollection, value); }

        private Axis[] _labels;
        public Axis[] Labels { get => _labels; set => SetAndNotify(ref _labels, value); }

        private Axis[] _yAxes;
        public Axis[] YAxes
        {
            get { return _yAxes; }
            set { SetAndNotify(ref _yAxes, value); }
        }

        public Func<double, string> Formatter { get; set; }

        public Brush WeekBorderBrush { get; set; }
        public Brush WeekBackground { get; set; }
        public Brush MonthBorderBrush { get; set; }
        public Brush MonthBackground { get; set; }
        public Brush YearBorderBrush { get; set; }
        public Brush YearBackground { get; set; }

        public string ProvicerTitle
        {
            get
            {
                return Globalspace.ProvicerTitle;
            }
        }

        public PzReportViewModel(IWindowManager windowManager, List<Common.Models.WeighingRecord> list)
        {
            this.windowManager = windowManager;
            Getdata(list);
        }

        public PzReportViewModel(IWindowManager windowManager, Common.Models.WeighingRecord record)
        {
            this.windowManager = windowManager;
            GetDataAsync(record);
        }

        public async void GetDataAsync(Common.Models.WeighingRecord record)
        {
            BrushConverter conv = new BrushConverter();
            Brush FEF03E = conv.ConvertFromInvariantString("#FEF03E") as Brush;
            Brush F5F7F9 = conv.ConvertFromInvariantString("#F5F7F9") as Brush;
            Brush E1E6EC = conv.ConvertFromInvariantString("#E1E6EC") as Brush;

            WeekBorderBrush = E1E6EC;
            WeekBackground = F5F7F9;
            MonthBorderBrush = E1E6EC;
            MonthBackground = F5F7F9;
            YearBorderBrush = E1E6EC;
            YearBackground = F5F7F9;

            using var db = AJDatabaseService.GetDbContext();
            var ch = record.Ch;
            var pages = await db.WeighingRecords.Where(p => p.Ch == ch && p.IsFinish)
                .OrderBy(p => p.AutoNo).Take(100).AsNoTracking().ToListAsync();

            if (pages.Count <= 0) return;

            var endDate = DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd 23:59:59"));

            YearBorderBrush = FEF03E;
            YearBackground = FEF03E;
            var paint = LegendTextPaint = new SolidColorPaint(new SKColor(_titleBrush.Color.R, _titleBrush.Color.G, _titleBrush.Color.B));
            Labels = new Axis[]
            {
                new Axis
                {
                    Labels = pages.Select(p=>p.Pzrq.GetValueOrDefault().ToString("yyyy-MM-dd")).ToList(),
                    NamePaint = paint,
                    LabelsPaint = paint,
                }
            };
            YAxes = new Axis[]
            {
                new Axis
                {
                    NamePaint = paint,
                    LabelsPaint = paint,
                }
            };
            var values = pages.Select(p => p.Pz).ToList();

            SeriesCollection = new ISeries[]
            {
                new LineSeries<decimal>
                {
                    Values = values,
                    Name = "皮重"
                }
            };

            //Formatter = value => value.ToString() + "T";
            Formatter = value => value.ToString();
        }

        public void Getdata(List<Common.Models.WeighingRecord> list)
        {
            BrushConverter conv = new BrushConverter();
            Brush FEF03E = conv.ConvertFromInvariantString("#FEF03E") as Brush;
            Brush F5F7F9 = conv.ConvertFromInvariantString("#F5F7F9") as Brush;
            Brush E1E6EC = conv.ConvertFromInvariantString("#E1E6EC") as Brush;

            WeekBorderBrush = E1E6EC;
            WeekBackground = F5F7F9;
            MonthBorderBrush = E1E6EC;
            MonthBackground = F5F7F9;
            YearBorderBrush = E1E6EC;
            YearBackground = F5F7F9;

            if (list == null || list.Count <= 0) return;


            var endDate = DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd 23:59:59"));

            YearBorderBrush = FEF03E;
            YearBackground = FEF03E;

            var dateGroup = list.GroupBy(g => (new
            {
                g.Jzrq.GetValueOrDefault().Year,
                g.Jzrq.GetValueOrDefault().Month
            })).ToList();


            Labels = new Axis[]
            {
                new Axis
                {
                    Labels = dateGroup.Select(p=>$"{(p.Key.Year)}-{(p.Key.Month)}").ToList()
                }
            };
            var values = new List<decimal>();

            foreach (var date in dateGroup)
            {
                values.Add(date.Sum(s => s.Jz));
            }
            //Formatter = value => value.ToString() + "T";
            Formatter = value => value.ToString();

            SeriesCollection = new ISeries[]
            {
                new LineSeries<decimal>
                {
                    Values = values,
                    Name = "净重"
                }
            };


        }

    }
}
